/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.servicecenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.servicecenter.domain.AssociationApply;
import com.jeeplus.servicecenter.service.AssociationApplyService;
import com.jeeplus.servicecenter.service.dto.AssociationApplyDTO;
import com.jeeplus.sys.domain.Post;
import com.jeeplus.sys.domain.Role;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.service.PostService;
import com.jeeplus.sys.service.RoleService;
import com.jeeplus.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 用户Controller
 *
 * @author jeeplus
 * @version 2021-8-29
 */
@Api(tags = "入会申请")
@RestController
@RequestMapping("/apply/association")
public class AssociationApplyController {


    @Autowired
    private AssociationApplyService associationApplyService;

    @Autowired
    private UserService userService;

    @Autowired
    private PostService postService;

    @Autowired
    private RoleService roleService;

    /**
     * 查询组织
     *
     * @param
     * @return
     */
    @DemoMode
    @ApiLog("查询组织")
    @ApiOperation(value = "查询组织")
    @PreAuthorize("hasAnyAuthority('apply:association:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<Post>> list( Page<Post> page) {
        IPage<Post> result = postService.page(page, new LambdaQueryWrapper<Post>().notLike(Post::getName, "无")
                .notLike(Post::getName, "开发岗"));
        return ResponseEntity.ok (result);
    }
    /**
     * 保存用户
     *
     * @param associationApplyDTO
     * @return
     */
    @DemoMode
    @ApiLog("入会申请")
    @ApiOperation(value = "提交申请")
    @PreAuthorize("hasAnyAuthority('apply:association:membership')")
    @PostMapping("membership")
    public ResponseEntity saveApply(@Valid @RequestBody AssociationApplyDTO associationApplyDTO) {
        if(associationApplyDTO.getName().isEmpty()){
            return ResponseEntity.badRequest ( ).body ( "请完善个人基本信息" );
        }
        if (!associationApplyDTO.isCheckLoginName()) {
            return ResponseEntity.badRequest ( ).body ( "当前登录用户和申请用户不一致！" );
        }
        if(associationApplyService.getOne(new QueryWrapper<AssociationApply>().eq("login_name", associationApplyDTO.getCurrentLoginName()).eq("result","未处理")) != null){
            return ResponseEntity.badRequest ( ).body ( "当前用户存在未处理申请，请处理后再操作！" );
        }
        Role roleOne = roleService.getOne(new LambdaUpdateWrapper<Role>().eq(Role::getPostId, associationApplyDTO.getPostId()).like(Role::getName, "会员"));
        associationApplyDTO.setRoleId(roleOne.getId());
        associationApplyService.saveApply(associationApplyDTO);
        return ResponseEntity.ok ( "入会申请成功!" );
    }

    /**
     * 保存用户
     *
     * @param associationApplyDTO
     * @return
     */
    @DemoMode
    @ApiLog("入会申请")
    @ApiOperation(value = "提交申请")
    @PostMapping("auto")
    public ResponseEntity autoAgree(@Valid @RequestBody AssociationApplyDTO associationApplyDTO) {
        if(associationApplyService.getOne(new QueryWrapper<AssociationApply>().eq("login_name", associationApplyDTO.getCurrentLoginName()).eq("result","未处理")) != null){
            return ResponseEntity.badRequest ( ).body ( "当前用户存在未处理申请，请处理后再操作！" );
        }
        User one = userService.getOne(new QueryWrapper<User>().eq("login_name", associationApplyDTO.getLoginName()).eq("del_flag","0"));

        AssociationApply associationApply = associationApplyDTO.getAssociationApply();
        associationApply.setResult("已同意");
        associationApply.setDealTime(new Date());
        if(one!=null){
            associationApply.setId(one.getId());
        }
        associationApplyService.saveOrUpdate(associationApply);
        return ResponseEntity.ok ( "入会申请成功!" );
    }
}
